<template>
  <div class="home-container">
    <el-container>
      <el-header>
        <div class="header-left">
          <img src="../assets/heima.png" />
          <span>杭州前端16期后台管理系统</span>
        </div>
        <el-button @click="logout">退出</el-button>
      </el-header>
      <el-container>
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <div @click="isCollapse=!isCollapse"
               class="toggleMenu">|||</div>
          <el-menu class="el-menu-vertical-demo"
                   background-color="#373d41"
                   text-color="#fff"
                   :unique-opened="true"
                   :collapse="isCollapse"
                   :collapse-transition="false"
                   router
                   :default-active="$route.path"
                   active-text-color="#409eff">
            <el-submenu :index="item.id+''"
                        v-for="item in menuList"
                        :key="item.id">
              <template slot="title">
                <i :class="iconsList[item.id]"></i>
                <span>{{ item.authName }}</span>
              </template>
              <el-menu-item :index="'/'+item2.path"
                            v-for="item2 in item.children"
                            :key="item2.id">
                <i class="el-icon-menu"></i>
                <span>{{ item2.authName }}</span>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data () {
    return {
      menuList: [],
      iconsList: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false
    }
  },
  created () {
    this.getMenuList()
  },

  methods: {
    logout () {
      sessionStorage.removeItem('token')
      this.$router.push('/login')
    },
    async getMenuList () {
      const { data: res } = await this.$axios.get('menus')
      if (res.meta.status !== 200) return this.$message.error('获取数据失败')
      this.menuList = res.data
    }
  }
}
</script>

<style scoped lang='less'>
.home-container {
  height: 100%;

  .el-container {
    height: 100%;

    .el-header {
      background-color: #373d41;
      display: flex;
      justify-content: space-between;
      align-items: center;

      .header-left {
        display: flex;
        align-items: center;

        img {
          height: 50px;
          filter: brightness(3000%);
        }

        span {
          font-size: 26px;
          color: #fff;
          margin-left: 10px;
        }
      }
    }

    .el-aside {
      background-color: #373d41;
      .iconfont {
        margin-right: 10px;
      }
      .el-menu {
        border-right: none;
      }
      .toggleMenu {
        width: 100%;
        height: 30px;
        line-height: 30px;
        text-align: center;
        color: #fff;
        background: skyblue;
        letter-spacing: 8px;
        cursor: pointer;
        user-select: none;
      }
    }

    .el-main {
      background-color: #eee;
    }
  }
}
</style>
